/*
    @Author: feilong
    @Time: 2019-10
    @Tittle: bodyTab
    @Description: 登录
*/

class Login {
    constructor(form, layer){
        this.form = form
        this.layer = layer
        this.name = ""
        this.password = ""
    }

    init(){
        //判断是否存在过用户
        var storage = window.localStorage;
        if("yes" == storage["has_info"]){          
            $("#user_name").val(storage["user_name"]);
            $("#password").val(storage["password"]);      
        };
        
    }

    saveInfo(){
        //存储到loaclStage
        var storage = window.localStorage;
        storage["user_name"] = $("#user_name").val();
        storage["password"] = $("#password").val();
        storage["has_info"] = "yes"
       
    }

    loginReq(data){
        //弹出loading
        this.tip1= top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});        
        var currentCookie = document.cookie
        console.log("______data___",JSON.stringify(data.field), currentCookie)  
        var content = JSON.stringify(data.field);
        var self = this
        $.get("./login_user", {content:content}, function(res, status){
            // res = JSON.parse(res);
            console.log("___________get__", res, status);
            self.loginRes(res, status)

        },"json");
    }

    loginRes(res, status){
        top.layer.close(this.tip1);
        if(res.error_code > 0){
            top.layer.msg(res.error_msg);
            return
        }        
        this.saveInfo();        
        window.location.href = "../../"
    }
}




layui.use(['form','layer','jquery'],function(){
    var form = layui.form;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    $ = layui.jquery;

    // $(".loginBody .seraph").click(function(){
    //     layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧",{
    //         time:5000
    //     });
    // })

   //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })

    //自定义验证规则
    form.verify({
        // user_name: function(value){
        //   if(value.length < 5){
        //     return '用户名至少得5个字符啊';
        //   }
        // },
        password: [
          /^[\S]{3,12}$/
          ,'密码必须3到12位，且不能出现空格'
        ]
    })     

    let login = new Login(form, layer)
    login.init()

    //登录按钮
    form.on("submit(login)",function(data){
        // $(this).text("登录中...").attr("disabled","disabled").addClass("layui-disabled");
        // setTimeout(function(){
        //     window.location.href = "/layuicms2.0";
        // },1000);
        // 
        login.loginReq(data)
        return false;
    })

})
